﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Clinica_Frba
{
    public class Conexion
    {
        private static Conexion instance;
        private string conString;

        private Conexion()
        {
            Utility util = Utility.getInstance();

            conString = util.getConnectionString();
        }

        public static Conexion getInstance()
        {
            if (instance == null)
                instance = new Conexion();

            return instance;
        }

        public System.Data.SqlClient.SqlConnection openCon()
        {
            SqlConnection sqlCon = new SqlConnection();
            sqlCon.ConnectionString = conString;
            sqlCon.Open();

            return sqlCon;
        }

        public bool isConnected()
        {
            try
            {
                SqlConnection sqlCon = openCon();
                sqlCon.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public SqlDataReader execQuery(string query)
        {
            SqlCommand command = new SqlCommand(query,openCon());

            return command.ExecuteReader();
        }

        public SqlDataAdapter execQueryGrid(string query)
        {
            SqlCommand command = new SqlCommand(query, openCon());
            SqlDataAdapter adapter = new SqlDataAdapter(command);

            return adapter;
        }

    }
}
